home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8549 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  91 lines

  1. Path: noc.tor.hookup.net!news
  2. From: Richard Steadman <rsteadma@micromedia.on.ca>
  3. Newsgroups: comp.lang.c
  4. Subject: Problem with realloc()
  5. Date: Mon, 04 Mar 1996 19:38:39 -0500
  6. Organization: Micromedia
  7. Message-ID: <313B8D0F.3109@micromedia.on.ca>
  8. NNTP-Posting-Host: keeper.mmltd.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. I wonder if anyone can spot the problem in the following code.
  15. The function readfile() is supposed to read a text file, and
  16. assign each line of it to a dynamic array. It has worked correctly
  17. for some files, but recently when realloc() is called, it causes a
  18. Bus error and core dump. I have no idea why. I have added a couple
  19. of printf() statements to pinpoint the problem, and it is
  20. definitely in the call to realloc().
  21.  
  22. Here's the code:
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. #define MINLINES 10     /* allocate storage for at least ? lines */
  28. #define MAXLINELEN 1000 
  29.  
  30. char **readfile(char *filename, int *size, int charsperline,
  31.      long maxfsize)
  32. {
  33.   register int asize=0;
  34.   long fsize=0;
  35.   FILE *fp;
  36.   char **array;
  37.   char **newarray;
  38.   long lines;
  39.   char *memarea;
  40.   int linelen;
  41.  
  42.   if ( (fp=fopen(filename,"r"))==NULL) return NULL;
  43.  
  44.   fseek(fp,0L,SEEK_END);
  45.   fsize=ftell(fp);        /* get file length */
  46.   fseek(fp,0L,SEEK_SET);
  47.  
  48.   if (fsize>maxfsize) return NULL;
  49.  
  50.   /* guess at number of lines: realloc later if nec. */
  51.   lines=fsize/charsperline;
  52.  
  53.   if (lines<MINLINES) lines=MINLINES;
  54.  
  55.   if ( (memarea=(char *) malloc(fsize+1) )==NULL)
  56.         /* +1 for \0 in last read */
  57.     return(NULL);
  58.  
  59.   if ( (array=malloc((size_t)(lines * sizeof(char *))) )==NULL){
  60.     free(memarea);
  61.     return(NULL);
  62.   }
  63.  
  64.   array[0]=memarea;
  65.  
  66.   while (fgets(array[asize],MAXLINELEN,fp)!=NULL){
  67.  
  68.     printf("fgets ok\n"); fflush(stdout);
  69.     linelen=strlen(array[asize]);
  70.     array[asize][linelen-1]='\0';
  71.     if (asize >= lines){
  72.         lines+=lines/2;        /* increase by 50% */
  73.         printf("reallocating\n"); fflush(stdout);
  74.         if ( (newarray=realloc(array,
  75.                       (size_t)(lines*sizeof(char *))))==NULL){
  76.             free(memarea);
  77.             free(array);
  78.             return NULL;
  79.         }
  80.         printf("done reallocating\n"); fflush(stdout);
  81.         array=newarray;
  82.     }
  83.     array[asize+1]=array[asize]+linelen;
  84.     asize++;
  85.     printf("EOL\n"); fflush(stdout);
  86.   }
  87.   
  88.   *size=asize;
  89.   return array;
  90. }
  91.